Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cartesian): timeslip preparation #1286

Merged
merged 42 commits into from
Aug 16, 2021
Merged

Conversation

monfera
Copy link
Contributor

@monfera monfera commented Aug 5, 2021

Summary

Non-functional code cleanup, code removal and type fixes. Also removes the large pixel dimensions with texture use.

BREAKING CHANGE

  • TextStyle.fontStyle is no longer a string, it's the more specific FontStyle
  • For symmetry, fontStyle in word cloud is also switching from string to FontStyle
  • Certain text configurations included both fill and textColor for the text color; fill got removed, because textColor is part of the public Font type, and because textColor has clearer meaning than fill. Yet, some of the code used the fill property and/or made the fill property also mandatory. So, userland code needs to remove some fill property, and might need to ensure that the correct value is going into textColor
  • getRadians got unpublished

Details

Reviewers,

  • commit 848e188d866be40c79bc545dbf4cac81c8f347ce contains the breaking change of switching to FontStyle
  • commit 80c743fa879003a986a2e78d0e297229e712edf2 contains the breaking change of phasing out fill
  • commit b544c0c9364b1e14c3e4816a6de120f92ba35e8f unpublishes getRadian (no apparent change to API MD)

Issues

Checklist

  • The proper chart type label was added (e.g. :xy, :partition) if the PR involves a specific chart type
  • The proper feature label was added (e.g. :interactions, :axis) if the PR involves a specific chart feature
  • Any consumer-facing exports were added to packages/charts/src/index.ts (and stories only import from ../src except for test data & storybook)
  • This was checked for cross-browser compatibility

@monfera monfera added :xy Bar/Line/Area chart related code quality labels Aug 5, 2021
@monfera monfera marked this pull request as draft August 5, 2021 22:03
@monfera monfera force-pushed the timeslip-prep branch 2 times, most recently from af9efea to 1d5aea6 Compare August 5, 2021 22:52
@monfera monfera added the :axis Axis related issue label Aug 5, 2021
@monfera monfera force-pushed the timeslip-prep branch 3 times, most recently from d3e3e2b to 848e188 Compare August 5, 2021 23:37
@monfera
Copy link
Contributor Author

monfera commented Aug 6, 2021

jenkins test this

@monfera monfera added :heatmap Heatmap/Swimlane chart related issue :wordcloud Wordcloud related issues labels Aug 6, 2021
@monfera
Copy link
Contributor Author

monfera commented Aug 6, 2021

jenkins test this

fontStyle: fontStyle ? (fontStyle as FontStyle) : 'normal',
fontWeight: 'normal',
fontStyle: fontStyle ?? 'normal',
fontWeight: 'bold',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could introduce an API option for font weight instead, then this bold can go back into the theme

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great idea

fontSize: 50,
fontFamily: 'custom-font-family',
fontStyle: 'custom-font-style',
fontStyle: 'italic',
Copy link
Contributor Author

@monfera monfera Aug 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's a real need for 'custom-font-style' we could introduce a fontShorthand or fontOverride property, the presence of which would bypass the current CSS shorthand string concatenation logic

*/
export const getRadians = (angle: number) => (angle * Math.PI) / 180;
/** @internal */
export const degToRad = (angle: Degrees): Radian => (angle / 180) * Math.PI;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unpublishing it as we don't currently expose pure utility functions in our API, and it wouldn't be a good fit for our library because the related areas take in angles in degrees, so the user needs not convert to radians

@monfera monfera force-pushed the timeslip-prep branch 2 times, most recently from 210f2d8 to cc63a3b Compare August 6, 2021 16:33
ctx.translate(x, y);
ctx.rotate(getRadians(rotation));
ctx.translate(-x, -y);
fn(ctx);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this function, b/c a single place used it, and it immediately reverted the last (negative) translate here

@monfera monfera requested a review from nickofthyme August 6, 2021 17:40
const height = rect.height - borderOffset * 2;

const borderWidth = !disableBorderOffset && stroke.width >= MIN_STROKE_WIDTH ? stroke.width : 0;
if (stroke.width >= MIN_STROKE_WIDTH) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bunch of 0.001's got replaced with MIN_STROKE_WIDTH

@monfera monfera requested review from markov00 and rshen91 August 7, 2021 00:44
x: 0,
y: 0,
rotate: 0,
x: chartRotation === 90 || chartRotation === 180 ? width : 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice clean up!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O(n) 🎉

Copy link
Contributor

@rshen91 rshen91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this clean up. Tested locally and changes look great!

@rshen91 rshen91 self-requested a review August 9, 2021 16:01
Copy link
Collaborator

@nickofthyme nickofthyme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, the vr tests do not like your changes 🤣 . Idk what that annotation story always triggers a small diff for all.

packages/charts/api/charts.api.md Show resolved Hide resolved
@@ -26,30 +27,28 @@ export function renderText(
origin: Point,
text: string,
font: TextFont,
degree: number = 0,
translation?: Partial<Point>,
angle: Degrees = 0,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

x: 0,
y: 0,
rotate: 0,
x: chartRotation === 90 || chartRotation === 180 ? width : 0,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O(n) 🎉

@@ -76,7 +76,16 @@ function createPattern(
if (fill) pCtx.fill(path);
}

return ctx.createPattern(patternCanvas, 'repeat') ?? undefined;
const pattern = ctx.createPattern(patternCanvas, 'repeat')!; // HTMLCanvasElement always yields a CanvasPattern anyway
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this will never ever throw?

Copy link
Contributor Author

@monfera monfera Aug 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't throw, as the spec says ctx.createPattern invoked with HTMLCanvasElement (which patternCanvas is guaranteed to be) has no null case.

Details:

It'd throw downstream of this function if pattern ended up being null (there are other throw conditions but TS guards against most(*) of those already; example: the "repeat" literal is OK here).

The spec for createPattern says:

The only potential for getting a null is point 2:

  1. If usability is bad, then return null

It's the preceding point that references usability:

  1. Let usability be the result of checking the usability of image

The "switch" at the link has no "bad" usability case for HTMLCanvasElement, so there can't be a null, so the ! is safe and pattern will never be null. Typescript (apparently) is just not granular enough here to recognize that certain argument types can't lead to certain result types.

(*) If my 2nd reading prompted by your question is correct, then a zero dimension canvas would lead to a DOM exception. TS doesn't complain about this possibility, nor does current master wrap it in throw/catch so this aspect is not made worse by this PR. Still, I'll check if we defend against a zero-dimension canvas upstream, eg. by not creating such a canvas, or by applying a minimum 1px by 1px dimension. The best combo is,avoiding a throw condition so that a throw/catch wrapper is not needed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zero dimension canvas would lead to a DOM exception

Good to know!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this and a bunch of other things here into #1303

@monfera monfera marked this pull request as ready for review August 12, 2021 22:03
@monfera
Copy link
Contributor Author

monfera commented Aug 16, 2021

jenkins test this

@monfera monfera mentioned this pull request Aug 16, 2021
19 tasks
@monfera
Copy link
Contributor Author

monfera commented Aug 16, 2021

jenkins test this

Copy link
Collaborator

@nickofthyme nickofthyme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest changes LGTM.

@monfera monfera enabled auto-merge (squash) August 16, 2021 18:52
@monfera monfera merged commit b2ae4f7 into elastic:master Aug 16, 2021
nickofthyme pushed a commit that referenced this pull request Aug 16, 2021
# [34.0.0](v33.2.4...v34.0.0) (2021-08-16)

### Code Refactoring

* **cartesian:** cartesian rendering iteration ([#1286](#1286)) ([b2ae4f7](b2ae4f7)), closes [#1202](#1202)

### BREAKING CHANGES

* **cartesian:** - `TextStyle.fontStyle` is no longer a `string`, it's the more specific `FontStyle`
- For symmetry, `fontStyle` in word cloud is also switching from `string` to `FontStyle`
- Certain text configurations included both `fill` and `textColor` for the text color; `fill` got removed, because `textColor` is part of the public `Font` type, and because `textColor` has clearer meaning than `fill`. Yet, some of the code used the `fill` property and/or made the `fill` property also mandatory. So, userland code needs to remove some `fill` property, and might need to ensure that the correct value is going into `textColor`
- `getRadians` got unpublished
- No attempt to draw a rect border if there's not enough width/height for at least the specified border width (ie. width/height being at least twice the border width)
@nickofthyme
Copy link
Collaborator

🎉 This PR is included in version 34.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:axis Axis related issue code quality :heatmap Heatmap/Swimlane chart related issue released Issue released publicly :wordcloud Wordcloud related issues :xy Bar/Line/Area chart related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants